package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Client;

@Repository(value = "clientDao")
@Transactional(readOnly = false)
public class ClientDaoImpl implements ClientDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	public List<Client> getListClient() {
		TypedQuery<Client> q = em.createNamedQuery("Client.findAll",Client.class);
    	return q.getResultList();
	}

	public List<Client> getListClientByActive(boolean active) {
		TypedQuery<Client> q = em.createNamedQuery("Client.findByActive",Client.class);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Client> getListClientByPharmacyIdByActive(Integer pharmacyId,boolean active) {
		TypedQuery<Client> q = em.createNamedQuery("Client.findByPharmacyIdByActive",Client.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Client> getListClientByNitByActive(String nit, boolean active) {
		TypedQuery<Client> q = em.createNamedQuery("Client.findByNitByActive",Client.class);
    	q.setParameter("nit",nit);
    	q.setParameter("active",active);
    	return q.getResultList();
	}
	
	public List<Client> getListClientByPharmacyIdByNitByActive(	Integer pharmacyId, String nit, boolean active) {
		TypedQuery<Client> q = em.createNamedQuery("Client.findByPharmacyIdByNitByActive",Client.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("nit",nit+ "%");
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Client> getListClientByPharmacyIdByNameByActive(Integer pharmacyId, String name, boolean active) {
		TypedQuery<Client> q = em.createNamedQuery("Client.findByPharmacyIdByNameByActive",Client.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("name",name+ "%");
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Client> getListClientByPharmacyIdByClientTypeByActive(Integer pharmacyId, String clientType, boolean active) {
		TypedQuery<Client> q = em.createNamedQuery("Client.findByPharmacyIdByClientTypeByActive",Client.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("clientType",clientType);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public Client findClientByClientId(Integer clientId) {
		Client res=null;
		try {
	    	res=em.find(Client.class,clientId);
		} catch (Exception e) {}
    	return res;
	}

	public void remove(Client client) {
		em.remove(em.getReference(Client.class,client.getClientId()));
	}

	public void update(Client client) {
		em.merge(client);
	}

	public void add(Client client) {
		em.persist(client);
	}

}
